home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / IFC_112 / netscape / application / Polygon.java < prev    next >
Encoding:
Text File  |  1999-05-28  |  3.6 KB  |  144 lines  |  [TEXT/CWIE]

  1. // Polygon.java
  2. // By Ned Etcode
  3. // Copyright 1996, 1997 Netscape Communications Corp.  All rights reserved.
  4.  
  5. package netscape.application;
  6.  
  7. import netscape.util.*;
  8.  
  9. // ALERT! Using a java.awt.Polygon here is not necessarily what you want.
  10.  
  11. /** Object subclass representing an ordered list of points.
  12.   */
  13.  
  14. public class Polygon implements Codable {
  15.     /* The total number of points in the Polygon. */
  16.     public int numPoints;
  17.  
  18.     /* The X-coordinates of the Polygon's points. */
  19.     public int xPoints[];
  20.  
  21.     /* The Y-coordinates of the Polygon's points. */
  22.     public int yPoints[];
  23.  
  24.     java.awt.Polygon awtPolygon;
  25.  
  26.     /** Constructs a Polygon with no vertices.
  27.       */
  28.     public Polygon() {
  29.         awtPolygon = new java.awt.Polygon();
  30.         update();
  31.     }
  32.  
  33.     /** Constructs a polygon with the given points.
  34.       */
  35.     public Polygon(int xPoints[], int yPoints[], int numPoints) {
  36.         awtPolygon = new java.awt.Polygon(xPoints, yPoints, numPoints);
  37.         update();
  38.     }
  39.  
  40.     /** Adds the point (<b>x</b>, <b>y</b>) to the end of the Polygon's list
  41.       * of points.
  42.       */
  43.     public void addPoint(int x, int y) {
  44.         awtPolygon.addPoint(x, y);
  45.         update();
  46.     }
  47.  
  48.     /** Returns the Polygon's bounding Rect.
  49.       */
  50.     public Rect boundingRect() {
  51.         java.awt.Rectangle box;
  52.         Rect rect;
  53.  
  54.         box = awtPolygon.getBoundingBox();
  55.         rect = new Rect(box.x, box.y, box.width, box.height);
  56.         update();
  57.  
  58.         return rect;
  59.     }
  60.  
  61.     /** Returns <b>true</b> if the Polygon contains the point
  62.       * (<b>x</b>, <b>y</b>).
  63.       */
  64.     public boolean containsPoint(int x, int y) {
  65.         boolean contains;
  66.  
  67.         contains = awtPolygon.inside(x, y);
  68.         update();
  69.  
  70.         return contains;
  71.     }
  72.  
  73.     /** Returns <b>true</b> if the Polygon contains the point <b>aPoint</b>.
  74.       */
  75.     public boolean containsPoint(Point aPoint) {
  76.         return containsPoint(aPoint.x, aPoint.y);
  77.     }
  78.  
  79.     /** Moves each of the Polygon's points by <b>deltaX</b> and <b>deltaY</b>.
  80.       */
  81.     public void moveBy(int deltaX, int deltaY) {
  82.         int     i;
  83.  
  84.         i = awtPolygon.npoints;
  85.         while (i-- > 0) {
  86.             awtPolygon.xpoints[i] += deltaX;
  87.             awtPolygon.ypoints[i] += deltaY;
  88.         }
  89.     }
  90.  
  91.     private void update() {
  92.         numPoints = awtPolygon.npoints;
  93.         xPoints = awtPolygon.xpoints;
  94.         yPoints = awtPolygon.ypoints;
  95.     }
  96.  
  97.     static final String XPOINTS = "xPoints";
  98.     static final String YPOINTS = "yPoints";
  99.  
  100.     /** Describes the Polygon class' information.
  101.       * @see Codable#describeClassInfo
  102.       */
  103.     public void describeClassInfo(ClassInfo info) {
  104.         info.addClass("netscape.application.Polygon", 1);
  105.         info.addField(XPOINTS, INT_ARRAY_TYPE);
  106.         info.addField(YPOINTS, INT_ARRAY_TYPE);
  107.     }
  108.  
  109.     /** Encodes the Polygon.
  110.       * @see Codable#encode
  111.       */
  112.     public void encode(Encoder encoder) throws CodingException {
  113.         if (numPoints == 0)
  114.             return;
  115.  
  116.         encoder.encodeIntArray(XPOINTS, xPoints, 0, numPoints);
  117.         encoder.encodeIntArray(YPOINTS, yPoints, 0, numPoints);
  118.     }
  119.  
  120.     /** Decodes the Polygon.
  121.       * @see Codable#decode
  122.       */
  123.     public void decode(Decoder decoder) throws CodingException {
  124.         int i, xs[], ys[];
  125.  
  126.         xs = decoder.decodeIntArray(XPOINTS);
  127.         ys = decoder.decodeIntArray(YPOINTS);
  128.  
  129.         if (xs == null || xs.length == 0)
  130.             return;
  131.  
  132.         numPoints = xs.length;
  133.  
  134.         for (i = 0; i < xs.length; i++)
  135.             addPoint(xs[i], ys[i]);
  136.     }
  137.  
  138.     /** Finishes the Polygon instance decoding.
  139.       * @see Codable#finishDecoding
  140.       */
  141.     public void finishDecoding() throws CodingException {
  142.     }
  143. }
  144.